package uk.ac.ebi.bioinvindex.model.impl.processing;


import uk.ac.ebi.bioinvindex.model.impl.StudyImpl;

import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Jul 27, 2007
 */
@Entity
@Table(name = "node")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "obj_type", discriminatorType = DiscriminatorType.STRING, length = 255)

public abstract class NodeImpl
		<UpstreamProcessing extends ProcessingImpl, DownstreamProcessing extends ProcessingImpl>
		extends GraphElementImpl {

	protected Collection<UpstreamProcessing> upstreamProcessings = new HashSet<UpstreamProcessing>();

	protected Collection<DownstreamProcessing> downstreamProcessings = new HashSet<DownstreamProcessing>();



	protected NodeImpl() {
		super();
	}

	protected NodeImpl(StudyImpl study) {
		super(study);
	}

	protected NodeImpl(StudyImpl study, boolean original) {
		super(study, original);
	}

	@Transient
	//ToDo: return immutable collections
	public Collection<UpstreamProcessing> getUpstreamProcessings() {
		return upstreamProcessings;
	}

	@Transient
	public Collection<DownstreamProcessing> getDownstreamProcessings() {
		return downstreamProcessings;
	}

	public void addUpstreamProcessing(UpstreamProcessing upstreamProcessing) {
		if (upstreamProcessing == null) {
			throw new IllegalArgumentException("upstreamProcessing cannot be null");
		}

		if (!upstreamProcessing.getInputNodes().contains(this)) {
			upstreamProcessing.addInputNode(this);
		}

		this.upstreamProcessings.add(upstreamProcessing);

	}

	public void addDownstreamProcessing(DownstreamProcessing downstreamProcessing) {
		if (downstreamProcessing == null) {
			throw new IllegalArgumentException("downstreamProcessing cannot be null");
		}

		if (!downstreamProcessing.getOutputNodes().contains(downstreamProcessing)) {
			downstreamProcessing.addOutputNode(this);
		}

		this.downstreamProcessings.add(downstreamProcessing);

	}
}
